﻿<%@ Page Language="C#" MasterPageFile="../../main.master" Inherits="VioletDevelopmentLaboratory.Net.Web.PageBase" %>
<%@ Import Namespace="System.Collections.ObjectModel" %>

<%@ Register src="modify.ascx" tagname="modify" tagprefix="uc1" %>

<script runat="server">
	private Board currentBoard;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (Request.UrlReferrer == null)
		{
			Response.Redirect("list.aspx");
		}

		currentBoard = Board.GetBoard(Request.QueryString["id"]);

		if (!Page.IsPostBack)
		{
			BoardDropDownListBind();
			CategoryDropDownListBind(BoardDropDownList.SelectedValue);
			ThreadGridViewBind();
		}
	}
	
	protected void BoardDropDownListBind()
	{
		BoardDropDownList.DataSource = Board.GetBoardCollection();
		BoardDropDownList.DataTextField = "Name";
		BoardDropDownList.DataValueField = "ComponentID";
		BoardDropDownList.DataBind();
	}

	protected void CategoryDropDownListBind(String componentID)
	{
		CategoryDropDownList.Items.Clear();
		
		Board destBoard = Board.GetBoard(componentID);

		CategoryDropDownList.DataSource = destBoard.GetCategoryCollection();
		CategoryDropDownList.DataTextField = "Name";
		CategoryDropDownList.DataValueField = "CategoryID";
		CategoryDropDownList.DataBind();
	}

	protected void ThreadGridViewBind()
	{
		Int32 pageIndex = 0;
		if (!String.IsNullOrEmpty(Request.QueryString["pageindex"]))
		{
			pageIndex = Int32.Parse(Request.QueryString["pageindex"]);
		}

		ThreadGridView.DataSource = currentBoard.GetThreadCollection(null, "", pageIndex);
		ThreadGridView.DataBind();

		ThreadGridViewPagerButton.PageSize = currentBoard.PageSize;
		ThreadGridViewPagerButton.ButtonCount = currentBoard.PageButtonCount;
		ThreadGridViewPagerButton.SelectCount = currentBoard.GetThreadCollectionCount(null, "");
	}

	protected void MoveButton_Click(object sender, EventArgs e)
	{
		foreach (GridViewRow _dr in ThreadGridView.Rows)
		{
			if (((CheckBox)_dr.Cells[0].FindControl("cb_Thread")).Checked)
			{
				currentBoard.GetThread(ThreadGridView.DataKeys[_dr.RowIndex].Value.ToString()).Move(
					BoardDropDownList.SelectedValue, CategoryDropDownList.SelectedValue);
			}
		}

		ThreadGridViewBind();

		VUtility.ShowAlert(Page, "선택된 글을 이동했습니다.");
	}

	protected void BoardDropDownList_SelectedIndexChanged(object sender, EventArgs e)
	{
		CategoryDropDownListBind(BoardDropDownList.SelectedValue);
	}

	protected void ThreadGridViewPagerButton_PageIndexChanged(object sender, PagerButtonPageIndexChangedEventArgs e)
	{
		Response.Redirect(e.NewPageUrl, true);
	}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="BodyContentPlaceHolder" runat="server">

	<h2>게시판 &gt; 목록 &gt; <%= String.Format("{0} ({1})", currentBoard.ComponentID, currentBoard.Name) %> &gt; 글이동</h2>

	<uc1:modify ID="modify1" runat="server" />

	<fieldset>
		<legend>글이동</legend>
		<table border="1" cellspacing="0" cellpadding="5" rules="all" style="border-collapse : collapse">
			<tr>
				<td>
					선택글을
					<asp:DropDownList ID="BoardDropDownList" runat="server" AutoPostBack="True"
						onselectedindexchanged="BoardDropDownList_SelectedIndexChanged" />
					<asp:DropDownList ID="CategoryDropDownList" runat="server" />로
					<asp:Button ID="MoveButton" runat="server" OnClick="MoveButton_Click" Text="이동" ValidationGroup="ThreadMove" />
					(주의:선택된 글과 같은 그룹의 글도 함께 이동됩니다.)
				</td>
			</tr>
		</table>
		<asp:GridView ID="ThreadGridView" runat="server" AutoGenerateColumns="False" 
			PageSize="40" Width="100%" EmptyDataText="글이 없습니다." DataKeyNames="ThreadID">
			<Columns>
				<asp:TemplateField HeaderText="선택" InsertVisible="False">
					<ItemTemplate>
						<asp:CheckBox ID="cb_Thread" runat="server" />
					</ItemTemplate>
					<ItemStyle HorizontalAlign="Center" />
				</asp:TemplateField>
				<asp:BoundField DataField="CategoryName" HeaderText="카테">
					<HeaderStyle Width="40px" />
				</asp:BoundField>
				<asp:TemplateField HeaderText="그룹">
					<HeaderStyle Width="40px" />
					<ItemTemplate>
						<span title="Group : <%# Eval("ThreadGroup") %> | ID : <%# Eval("ThreadID") %> | Depth : <%# Eval("ThreadDepth") %> | Index : <%# Eval("ThreadIndex") %>">
							<%# Eval("ThreadGroup") %>
						</span>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:BoundField HeaderText="아이디" DataField="ThreadID">
					<HeaderStyle Width="40px" />
				</asp:BoundField>
				<asp:TemplateField HeaderText="작성자">
					<ItemTemplate>
						<span title="<%# Eval("providerUserKey") %>" style="font-weight : bold;"><%# Eval("UserName") %></span>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="제목">
					<ItemTemplate>
						<a href="<%# Eval("PageUrl") %>" target="_blank"><%# Eval("Title") %></a>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:CheckBoxField DataField="IsSecret" HeaderText="비밀글">
					<HeaderStyle Width="40px" />
				</asp:CheckBoxField>
				<asp:BoundField DataField="RegistrationDate" HeaderText="등록일">
					<HeaderStyle Width="160px" />
				</asp:BoundField>
			</Columns>
		</asp:GridView>
		<VWeb:PagerButton ID="ThreadGridViewPagerButton" runat="server" 
			PageIndexQueryStringName="pageindex" 
			onpageindexchanged="ThreadGridViewPagerButton_PageIndexChanged">
			<QueryStringParameters>
				<VWeb:CustomQueryStringParameter Name="id" />
			</QueryStringParameters>
		</VWeb:PagerButton>
	</fieldset>

</asp:Content>